package com.ledim.utils;

import android.text.format.Time;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * User: howie
 * Date: 13-5-11
 * Time: 下午4:09
 */
public class TimeUtil {

    public static String timeAgo(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss zzz");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        long timeStamp = date.getTime();

        Date currentTime = new Date();
        long currentTimeStamp = currentTime.getTime();
        long seconds = (currentTimeStamp - timeStamp) / 1000;

        long minutes = Math.abs(seconds / 60);
        long hours = Math.abs(minutes / 60);
        long days = Math.abs(hours / 24);

        if (seconds < 60) {
            return "刚刚";
        } else if (seconds < 120) {
            return "1分钟前";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        } else if (minutes < 120) {
            return "1小时前";
        } else if (hours < 24) {
            return hours + "小时前";
        } else if (hours < 24 * 2) {
            return "1天前";
        } else if (days < 30) {
            return days + "天前";
        } else if (days < 365) {
            return new BigDecimal(days / 30).setScale(0, BigDecimal.ROUND_HALF_UP) + "个月前";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String dateString = formatter.format(date);
            return dateString;
        }

    }

    public static String getToday() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        String dateString = formatter.format(new Date());
        return dateString;
    }

    public static String timeLeft(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        long timeStamp = date.getTime();
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        Date currentTime = new Date();
        long currentTimeStamp = currentTime.getTime();

        long total_seconds = (timeStamp - currentTimeStamp) / 1000;

        if (total_seconds <= 0) {
            return "";
        }

        long days = Math.abs(total_seconds / (24 * 60 * 60));

        long hours = Math.abs((total_seconds - days * 24 * 60 * 60) / (60 * 60));
        long minutes = Math.abs((total_seconds - days * 24 * 60 * 60 - hours * 60 * 60) / 60);
        long seconds =
                Math.abs((total_seconds - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60));
        String leftTime;
        if (days > 0) {
            leftTime = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
        } else if (hours > 0) {
            leftTime = hours + "小时" + minutes + "分" + seconds + "秒";
        } else if (minutes > 0) {
            leftTime = minutes + "分" + seconds + "秒";
        } else if (seconds > 0) {
            leftTime = seconds + "秒";
        } else {
            leftTime = "0秒";
        }

        return leftTime;

    }

    public static String getTime() {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());
    }

    public static int timeToage(String time) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = format.parse(time);
            return getAge(date);
        } catch (Exception e) {
            e.printStackTrace();
            return -0;
        }
    }

    /**
     * 计算年龄
     */
    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                //monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                //monthNow>monthBirth
                age--;
            }
        }

        return age;
    }

    /**
     * 当周第一天
     *
     * @return
     */
    public static String getFirstDayfWeek(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(date);
        gcLast.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//设置周一
        gcLast.setFirstDayOfWeek(Calendar.SUNDAY);
        String day_first = df.format(gcLast.getTime());
        return day_first.toString();

    }

    /**
     * 当周最后一天
     *
     * @return
     */
    public static String getLastDayofWeek(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(date);
        gcLast.setFirstDayOfWeek(Calendar.SUNDAY);
        gcLast.set(Calendar.DAY_OF_WEEK, gcLast.getFirstDayOfWeek() + 6); // Sunday
        String day_first = df.format(gcLast.getTime());
        return day_first.toString();

    }

    /**
     * 当月第一天
     *
     * @return
     */
    public static String getFirstDayofMonth(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(date);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        day_first += " 00:00:00";
        return day_first.toString();

    }

    /**
     * 获取某月的最后一天
     *
     * @throws
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     */
    public static String getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();

        cal.setTime(date);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        lastDayOfMonth += " 23:59:59";

        return lastDayOfMonth;
    }

    /**
     * 某年第一天
     *
     * @return
     */
    public static String getFirstDayofYear(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(date);
        gcLast.set(Calendar.DAY_OF_YEAR, 1);
        String day_first = df.format(gcLast.getTime());
        day_first += " 00:00:00";
        return day_first.toString();

    }

    /**
     * 获取某年的最后一天
     *
     * @throws
     * @Title:getLastDayOfMonth
     * @Description:
     * @param: date
     * @return:String
     */
    public static String getLastDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();

        cal.setTime(date);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_YEAR, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        lastDayOfMonth += " 23:59:59";

        return lastDayOfMonth;
    }

    /**
     * 某一个月第一天和最后一天
     *
     * @param date
     * @return
     */
    private static int getDayOfMonth(Date date) {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.setTime(date);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;

    }

    /**
     * ******* Time *********
     */

    public static long currentTimeInMillis() {
        Time time = new Time();
        time.setToNow();
        return time.toMillis(false);
    }

    public static String cal(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        return unitFormat(h) + ":" + unitFormat(d) + ":" + unitFormat(s);
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + Integer.toString(i);
        } else {
            retStr = "" + i;
        }
        return retStr;
    }

    /**
     * @param timeStr
     * @return yyyy-MM-dd HH:mm
     */
    public static String time(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 根据时间获取周几
     *
     * @return
     */
    public static String dayForWeek() {
        Date date = new Date();
        long time = date.getTime();

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int dayForWeek = c.get(Calendar.DAY_OF_WEEK);
        String week = null;
        switch (dayForWeek) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
        }
        return week + " " + formatChatTime(time);
    }

    public static String formatChatTime(long time) {
        DateFormat formatter = new SimpleDateFormat("HH:mm");
        return formatter.format(new Date(time));
    }

    //返回格式 MM-dd
    public static String formatTime(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String formatVideoTime(long time) {
        DateFormat formatter = new SimpleDateFormat("mm:ss");
        return formatter.format(new Date(time));
    }

    public static String dateformatTime(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * return XX月XX日
     *
     * @param date
     * @return
     */
    public static String dateformatTime2(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * @param timeStr
     * @return -1 过去时间
     * 0 今天
     * 1 将来时间
     */
    public static int getTimeAgo(String timeStr) {
        long oneDay = 24 * 60 * 60 * 1000; // 一天时间

        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return 1;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(date);

        // 当前时间，从当天0时算起
        Date todayDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayTime = format.format(todayDate);
        Date today = null;
        try {
            today = format.parse(todayTime);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // 目标时间
        Date targetDate = null;
        try {
            SimpleDateFormat formatTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            targetDate = formatTime.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return 1;
        }

        if (targetDate.getTime() - today.getTime() >= 0 &&
                targetDate.getTime() - today.getTime() < oneDay) {
            return 0;
        } else if (targetDate.getTime() - today.getTime() >= oneDay) {
            return 1;
        } else if (targetDate.getTime() - today.getTime() < 0) {
            return -1;
        } else {
            return 1;
        }
    }

    /**
     * 根据时间获取周几
     *
     * @param pTime
     * @return
     */
    public static String dayForWeekStr(String pTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int dayForWeek = c.get(Calendar.DAY_OF_WEEK);
        String week = null;
        switch (dayForWeek) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
        }
        return week;
    }

    public static String getMonth(String pTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(pTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int month = date.getMonth() + 1;//月

        return month + "";
    }

    public static String getDay(String pTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(pTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int day = date.getDate();//日
        return day + "";
    }

    //返回格式 MM-dd
    public static String formatHistoryTime(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String dateToTime(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss ZZZ");
        String dateString = formatter.format(date.getTime());
        return dateString;
    }

    /**
     * @param date
     * @return yyyy/MM/dd
     */
    public static String dateToTime2(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String dateString = formatter.format(date.getTime());
        return dateString;
    }

    public static String timeToTime(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss zzzzz");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String formatDate(String timeStr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return formatter.format(timeStr);
    }

    public static String getDateToString(long time) {
        Date d = new Date(time * 1000);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /**
     * 获取聊天时间
     */
    public static String getChatTime(long time) {
        Date date = new Date(time);
        if (checkToday(date) >= 0) { // 当天时间
            DateFormat formatter = new SimpleDateFormat("HH:mm");
            return formatter.format(date);
        } else if (checkToday(date) == -1) {
            DateFormat formatter = new SimpleDateFormat("HH:mm");
            return "昨天 " + formatter.format(date);
        } else {
            int week = checkWeek(date);
            if (belongWeek(date, week) == 1) {
                String weekStr = "";
                switch (week) {
                    case 1:
                        weekStr = "星期一 ";
                        break;
                    case 2:
                        weekStr = "星期二 ";
                        break;
                    case 3:
                        weekStr = "星期三 ";
                        break;
                    case 4:
                        weekStr = "星期四 ";
                        break;
                    case 5:
                        weekStr = "星期五 ";
                        break;
                    case 6:
                        weekStr = "星期六 ";
                        break;
                    case 7:
                        weekStr = "星期日 ";
                        break;
                }
                DateFormat formatter = new SimpleDateFormat("HH:mm");
                return weekStr + formatter.format(date);
            } else {
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                return formatter.format(date);
            }
        }
    }

    /**
     * -2 昨天以前
     * -1 昨天
     * 0 今天
     * 1 将来时间
     */
    public static int checkToday(Date targetDate) {
        long oneDay = 24 * 60 * 60 * 1000; // 一天时间

        // 当前时间，从当天0时算起
        Date todayDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayTime = format.format(todayDate);
        Date today = null;
        try {
            today = format.parse(todayTime);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        if (targetDate.getTime() - today.getTime() >= 0 &&
                targetDate.getTime() - today.getTime() < oneDay) {
            return 0; // 今天
        } else if (targetDate.getTime() - today.getTime() >= oneDay) {
            return 1; // 今天以后
        } else if (targetDate.getTime() - today.getTime() < 0) {
            if (targetDate.getTime() > today.getTime() - oneDay) {
                return -1; // 昨天
            } else {
                return -2; // 昨天以前
            }

        } else {
            return 1;
        }

    }

    /**
     * 1 本周内
     * -1 本周外
     */
    public static int belongWeek(Date targetDate, int day) {
        long oneDay = 24 * 60 * 60 * 1000; // 一天时间
        Date todayDate = new Date();
        if ((todayDate.getTime() - targetDate.getTime()) > (oneDay * day)) {
            return -1;
        } else {
            return 1;
        }
    }

    public static int checkWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayForWeek = c.get(Calendar.DAY_OF_WEEK);
        int week = 1;
        switch (dayForWeek) {
            case 1:
                week = 7;
                break;
            case 2:
                week = 1;
                break;
            case 3:
                week = 2;
                break;
            case 4:
                week = 3;
                break;
            case 5:
                week = 3;
                break;
            case 6:
                week = 4;
                break;
            case 7:
                week = 6;
                break;
        }
        return week;
    }

}
